MCS-51

Tutorials

EX-F320

LED Timer
Serial

C8051F340/380

LED Timer
Serial

C8051F V2.1 (F020)

LED Timer
Serial

C8051F300 Dev. B. Mod.

LED Timer
Serial

C8051F V2.1 (F120)

LED Timer
Serial
Benchmarks

51 MCU SCM

Serial

STC89 DEMO BOARD

LED Timer
Serial

EZ-USB FX2LP

LED Timer
Serial
Benchmarks

colecovision.eu

ColecoVision

STM8

MCS-51

LLVM+SDCC

Contact

Getting started with MCS-51 development using free software: Serial output on the EZ-USB FX2LP

This short tutorial presents a simple "Hello World" program for the CY7C68013A EZ-USB FX2LP USB2.0 Development Board with an extra RS232 board for serial communication. Such boards are widely available via different channels. The one used here was bought on ebay, while the RS232 board was bought from WaveShare. The author used a Debian GNU/Linux system, but the tutorial should work for other Linux distributions, *BSD or other Unices.

The tools we use are

Hardware setup

CY7C68013A EZ-USB FX2LP USB2.0 Development Board with RS232 board

The STM8L-DISCOVERY is connected to the host computer via a USB cable for power and to write the demo onto the board. We connect the RS232 board to the PA0 header for data and to the power header for power (VCC to +5V, GND to GND); the serial cable is then attached to the RS232 board. On the other end the serial cable is attached to an RS232 port on a computer running a terminal program configured for 1200 baud, no parity, 8 bits, 1 stop bit and no flow control. We used a USB-to-serial converter and gtkterm.

Get SDCC

Depending on your operating system there might be an easy way to install SDCC 3.5.0 or newer using a package system or similar (e.g. apt-get install sdcc on Debian). While SDCC 3.4.0 should be sufficient for this tutorial, you might want to try a newer version in case you encounter any bugs.

SDCC binaries or a source tarball can be downloaded from its website.

Get cycfx2prog

Depending on your operating system there might be an easy way to install cycfx2prog using a package system or similar (e.g. apt-get install sdcc on Debian).

A source tarball can be downloaded from its website.

The Demo

We present a simple Demo that repeatedly outputs "Hello World!". Since the versions of the FX2LP with low pin-count do not have an UART, we need to emulate one in software. Here is the C code:

// Source code under CC0 1.0
#include <stdbool.h>
#include <stdio.h>

__sfr __at(0x80) IOA;
__sfr __at(0xb2) OEA;

__sfr __at(0x88) TCON;
__sfr __at(0x89) TMOD;
__sfr __at(0x8b) TL1;
__sfr __at(0x8d) TH1;

__sfr __at(0xa8) IE;

volatile unsigned char sendcounter;
volatile unsigned int senddata;
volatile _Bool sending;

void send_bit(void) __interrupt(3)
{
	TH1 = (65536 - 833) / 256;
	TL1 = (65536 - 833) % 256;

	if(!sending)
		return;

	IOA = senddata & 1;
	senddata >>= 1;

	if(!--sendcounter)
	{
		sending = false;
		IE &= ~0x08;
	}
}

int putchar(int c)
{
	while(sending);

	senddata = (c << 1) | 0x200;

	sendcounter = 10;

	sending = true;
	IE |= 0x08;

	return (c);
}

void main(void)
{
	unsigned long int i = 0;

	// Configure timer for 1200 baud
	TH1 = (65536 - 833) / 256;
	TL1 = (65536 - 833) % 256;
	TMOD = 0x10;
	IE |= 0x80;
	TCON |= 0x40; // Start timer

	OEA=0x01; // port A as output

	
	for(;;)
	{
		printf("Hello World!\n");
		for(i = 0; i < 147456; i++); // Sleep
	}
}

SDCC is a freestanding, not a hosted implemenatation of C, and allows main to return void. In SDCC up to SDCC 3.6.0 putchar() used to return void. This is not standard compliant and was changed to int in current SDCC versions. The printf() from the standard library uses putchar() for output. Since putchar() is device-specific we need to supply it. In this case we want it to output data using our software UART.

The demo can be compiled simply by invocing SDCC using sdcc -mmcs51 --std-c99 serial.c assuming the C code is in serial.c. The option -mstm8 selects the target port (stm8). An .ihx file with a name corresponding to the source file will be generated.

Put the demo onto the board

Assuming the board is attached via USB, cycfx2prog prg:led.ihx run will write the demo onto the board. You can see the "Hello world" by attaching a serial cable to the DB9 connector on the RS232 board, and using a terminal program configured for 1200 baud, no parity, 8 bits, 1 stop bit and no flow control.


More about cycfx2prog

cycfx2prog was written by Wolfgang Wieser. It is a tool to program the FX2 (i.e. to download '8051 firmware into the RAM) and do basic endpoint communication for testing purposes. cycfx2prog requires the input file to be an .ihx Intel hex file.

More about SDCC

SDCC was initially written by Sandeep Dutta for the MCS-51, and has a relatively conservative architecture (see Sandeep Dutta, "Anatomy of a Compiler", 2000). It has been extended by various contributors and more recently, incorporated some cutting-edge technologies, in particular in register allocation (see Philipp Klaus Krause, "Optimal Register Allocation in Polynomial Time", 2013 and "Bytewise Register Allocation", 2015). However the mcs51 backend does not have all the fancy features and optimizations that some newer backends have.

SDCC is a C compiler that aims to be compliant with the C standards.

Important compiler options for MCS-51 developers include: